package com.bpz.jxw.user.service.impl;

import com.bpz.jxw.article.model.NewArticle;
import com.bpz.jxw.article.service.INewArticleService;
import com.bpz.jxw.user.dao.UserDao;
import com.bpz.jxw.user.model.User;
import com.bpz.jxw.user.service.IUserService;
import com.bpz.main.bean.ImgInfo;
import com.bpz.main.model.ActMsg;
import com.bpz.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

/**
 * Created by 半瓶子 on 2/28/15.
 * blog: xusir.cnblogs.com
 */
@Service
public class UserService implements IUserService {

    @Autowired
    UserDao userDao;
    @Autowired
    INewArticleService newArticleService;

    /**
     * 用户登录
     *
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @Override
    public ActMsg login(String userName, String password) {
        ActMsg msg = new ActMsg();
        if (StringUtils.isEmpty(userName)) {
            msg.setCode(ActMsg.ERROR);
            msg.setMsg("用户名不能为空！");
            return msg;
        }
        if (StringUtils.isEmpty(password)) {
            msg.setCode(ActMsg.ERROR);
            msg.setMsg("密码不能为空！");
            return msg;
        }
        User user = new User();
        user.setNickname(userName);
        List<User> users = userDao.select(user);
        if (users == null || users.size() == 0) {
            msg.setCode(ActMsg.ERROR);
            msg.setMsg("用户错误或用户不存在！");
            return msg;
        }
        if (users != null && users.size() == 1) {
            user = users.get(0);
            password = StringUtils.md5(password);
            if (!password.equals(user.getPwd())) {
                msg.setCode(ActMsg.ERROR);
                msg.setMsg("密码错误！");
                return msg;
            } else if (0 == user.getStatus()) {
                msg.setCode(ActMsg.ERROR);
                msg.setMsg("用户被禁止登录，请联系客服！");
                return msg;
            } else if (0 == user.getEmailstatus()) {
                msg.setCode(ActMsg.ERROR);
                msg.setMsg("您的邮箱需要验证！已向您的邮箱发送了验证邮件。");
                return msg;
            }
            User u = new User();
            u.setUid(user.getUid());
            u.setLastIp(StringUtils.getIP());
            u.setLastTime(DateUtils.getNowTimeSecond());
            int res = userDao.update(u);
            if (res == 1) {
                SessionUtils.getInstance().set("user_session", user);
                msg.setCode(ActMsg.SUCCESS);
                msg.setMsg("登录成功");
            } else {
                msg.setCode(ActMsg.ERROR);
                msg.setMsg("登录异常！");
            }
        } else {
            msg.setCode(ActMsg.ERROR);
            msg.setMsg("帐户异常，请联系客服！");
        }
        return msg;
    }

    /**
     * 用户注册
     *
     * @param username
     * @param password
     * @param email
     * @return
     */
    @Override
    public ActMsg reg(String username, String password, String email) {
        ActMsg msg = new ActMsg();
        msg.setCode(ActMsg.ERROR);
        if (StringUtils.isEmpty(username)) {
            msg.setMsg(PropCnfg.getVal("usernameNotNull"));
            return msg;
        }
        if (StringUtils.isEmpty(password) || password.length() < 6) {
            msg.setMsg(MessageFormat.format(PropCnfg.getVal("pwdNotNull"), 6));
            return msg;
        }
        if (ValidateUtils.isEmail(email)) {
            msg.setMsg(PropCnfg.getVal("notEmail"));
            return msg;
        }

        User user = new User();
        user.setNickname(username);
        List<User> users = userDao.select(user);
        if (users != null && users.size() > 0) {
            msg.setMsg(PropCnfg.getVal("nicknameExist"));
            return msg;
        }
        user.setNickname(null);
        user.setEmail(email);
        users = userDao.select(user);
        if (users != null && users.size() > 0) {
            msg.setMsg(PropCnfg.getVal("emailExist"));
            return msg;
        }
        user.setNickname(username);
        user.setEmail(email);
        user.setLastIp(0l);
        user.setLastTime(0L);
        user.setEmailstatus(0);
        user.setPwd(StringUtils.md5(password));
        user.setRegIp(StringUtils.getIP());
        user.setRegTime(DateUtils.getNowTimeSecond());
        user.setStatus(1);
        user.setAvatarSuffix("");
        try {
            int res = userDao.insert(user);
            if (res == 1) {
                SessionUtils.getInstance().set("user_session", user);
                msg.setCode(ActMsg.SUCCESS);
                msg.setMsg(PropCnfg.getVal("regsuccess"));
            } else {
                msg.setMsg(PropCnfg.getVal("regfail"));
            }
        } catch (Exception ex) {
            msg.setMsg(PropCnfg.getVal("regexception"));
        }
        return msg;
    }

    /**
     * 获取登录用户信息
     * @return
     */
    public static User getLoginUser() {
        Object u = SessionUtils.getInstance().get("user_session");
        if (u != null) {
            return (User) u;
        }
        return null;
    }

    /**
     * 讲一个
     *
     * @param content     内容
     * @param video       视频地址
     * @param tag         标签
     * @param isAnonymous 是否匿名
     * @param img         图片地址
     * @return
     */
    @Override
    public ActMsg talk(int uid, String content, String video, String tag, int isAnonymous, ImgInfo img,int type) {
        ActMsg msg = new ActMsg();
        msg.setCode(ActMsg.ERROR);
        if (StringUtils.isEmpty(content)) {
            msg.setMsg(PropCnfg.getVal("contentNotNull"));
            return msg;
        } else if (content.length() < 20) {
            msg.setMsg(PropCnfg.getVal("contentLess"));
            return msg;
        } else if (content.length() > 2000) {
            msg.setMsg(PropCnfg.getVal("contentMore"));
            return msg;
        }
        if (!StringUtils.isEmpty(tag) && tag.length() > 20) {
            msg.setMsg(PropCnfg.getVal("tagMore"));
            return msg;
        }
        NewArticle article = new NewArticle();
        article.setType(type);
        article.setStatus(1);
        article.setBelow(0);
        article.setContent(content);
        article.setIp(StringUtils.getIP());
        article.setIs_anonymous(isAnonymous);
        article.setPic_height(img.getHeight());
        article.setPic_width(img.getWidth());
        article.setPic_url(img.getPath());
        article.setReply(0);
        article.setTag(tag);
        article.setTime(DateUtils.getNowTimeSecond());
        article.setTitle("");
        article.setUid(uid);
        article.setUpper(0);
        article.setVideo(video);
        msg = newArticleService.insert(article);
        return msg;
    }
}
